﻿using System;
using System.Net.Http;
using System.Threading.Tasks;
using SmartApi.Utils.Serialize;

namespace SmartApi
{
    public class ApiClient
    {
        private readonly HttpClient _httpClient;
        private readonly IPrepareRequest _prepareRequest;
        private readonly IParseResponse _parseResponse;

        public ApiClient(HttpClient httpClient
            , IPrepareRequest prepareRequest
            , IParseResponse parseResponse)
        {
            _httpClient = httpClient;
            _prepareRequest = prepareRequest;
            _parseResponse = parseResponse;
        }

        public async Task<T> SendAsync<T>(RequestContext requestContext)
        {
            await _prepareRequest.Prepare(requestContext);
            var resp = await _httpClient.SendAsync(requestContext.HttpRequest);
            return await _parseResponse.Parse<T>(requestContext);
        }
    }
}
